package com.beyondsoft.mcms.domain;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.TypedQuery;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.annotations.Type;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findItemsByCategoryAndIsValid", "findItemsByCategory" })
public class Item {
	@Id
	@Column(name = "id")
	private Long id;

	/**
     */
	@NotNull
	@Size(min = 1)
	@Column(unique = true)
	private String name;

	/**
     */
	@NotNull
	@Type(type = "java.lang.Boolean")
	private Boolean isValid = true;

	/**
     */
	@NotNull
	@ManyToOne
	private Category category;

	public String toString() {
		return (getIsValid() ? getName() : "");
	}

	public static Item getMaxItemByCategory(Category category) {
		TypedQuery<Item> q = entityManager().createQuery("SELECT max(o) FROM Item o where o.category = :category ",
				Item.class);
		q.setParameter("category", category);
		return q.getSingleResult();
	}
}
